/*
 * Copyright (c)  2018, Marmot
 * All rights reserved. 
 *
 * Id:SftpApplication.java   2018-06-29 16:03 wanglijun
 */
package io.marmot;

import com.jcraft.jsch.ChannelSftp;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.*;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.file.FileNameGenerator;
import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.sftp.filters.SftpSimplePatternFileListFilter;
import org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizer;
import org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizingMessageSource;
import org.springframework.integration.sftp.outbound.SftpMessageHandler;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;

import java.io.File;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2018
 * </p>
 * <p>
 * Company: Marmot
 * </p>
 *
 * @author: wanglijun
 * @create: 2018-06-29 16:03
 * @version：1.0
 */
@SpringBootApplication
public class SftpApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context =new  SpringApplicationBuilder (SftpApplication.class).web (false).run (args);
        MyGateway gateway = context.getBean(MyGateway.class);
        gateway.sendToSftp(new File("/foo/bar.txt"));
    }

    @Bean
    public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory(){
        DefaultSftpSessionFactory factory=new DefaultSftpSessionFactory (true);
        factory.setHost ("106.14.183.242");
        factory.setPort (22);
        factory.setUser ("root");
        factory.setPassword ("Accp1234");
        factory.setAllowUnknownKeys (true);
        return new CachingSessionFactory<ChannelSftp.LsEntry> (factory);
    }

    @Bean
    public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
        SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
        fileSynchronizer.setDeleteRemoteFiles(false);
        fileSynchronizer.setRemoteDirectory("/root");
        fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter ("*.xml"));
        return fileSynchronizer;
    }

    @Bean
    @InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "5000"))
    public MessageSource<File> sftpMessageSource() {
        SftpInboundFileSynchronizingMessageSource source =
                new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
        source.setLocalDirectory(new File("sftp-inbound"));
        source.setAutoCreateLocalDirectory(true);
        source.setLocalFilter(new AcceptOnceFileListFilter<File> ());
        source.setMaxFetchSize(1);
        return source;
    }

//    @Bean
//    @ServiceActivator(inputChannel = "sftpChannel")
//    public MessageHandler handler() {
//        return new MessageHandler() {
//            @Override
//            public void handleMessage(Message<?> message) throws MessagingException {
//                System.out.println(message.getPayload());
//            }
//
//        };
//    }

    @Bean
    @ServiceActivator(inputChannel = "toSftpChannel")
    public MessageHandler handler() {
        SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
        handler.setRemoteDirectoryExpressionString("headers['remote-target-dir']");
        handler.setFileNameGenerator(new FileNameGenerator () {

            @Override
            public String generateFileName(Message<?> message) {
                return "handlerContent.test";
            }

        });
        return handler;
    }


    @MessagingGateway
    public interface MyGateway {
        @Gateway(requestChannel = "toSftpChannel")
        void sendToSftp(File file);

    }

}
